import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Student {
    String stuNum;
    String name;
    String sex;
//    Date birthDate;
    String birthYearMonth;
//    2000-01

    public  static  final String  MALE="男";
    public  static  final String  FEMALE="女";

    //格式：年-月
    public static final String fmtYm = "yyyy-MM";

    /**
     * 把符合日期格式的字符串转换为日期类型
     */
    public static Date stringtoDate(String dateStr, String format) {
        Date d = null;
        SimpleDateFormat formater = new SimpleDateFormat(format);
        try {
            formater.setLenient(false);
            d = formater.parse(dateStr);
        } catch (Exception e) {
            // log.error(e);
            d = null;
        }
        return d;
    }

    /**
     * 返回日期的年
     */
    public static int getYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }

//    https://blog.csdn.net/kiss_the_sun/article/details/6831051
     public  int getAge(){
//        Date date = DateUtil.stringtoDate(birthYearMonth, DateUtil.MONTG_DATE_FORMAT);
        Date date = stringtoDate(birthYearMonth,fmtYm);
        Date now = new Date();
//        Date.
        int yearNow = getYear(now);
        int yearBirth = getYear(date);
        return  yearNow-yearBirth;

//        return     DateUtil.dayDiff(now,date);
//        return  now-date;
//        Date date=new Date(Long.parseLong(time));
//        SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        time=formatter.format(date);

//————————————————
//        版权声明：本文为CSDN博主「kiss_the_sun」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
//        原文链接：https://blog.csdn.net/kiss_the_sun/article/details/6831051
    }
}
